#include "request-creator-server.hh"

#include "request-server.hh"

#include <request/request-decorator.hh>

RequestCreatorServer::RequestCreatorServer (const int* clientid, Client* c):
  RequestCreator (),
  id_ (clientid),
  c_ (c)
{
}

boost::shared_ptr<Request>
RequestCreatorServer::operator() (RequestType rqtype, const std::string& data)
{
  // std::cout << "RECEIVED RTYPE = " << (int) rqtype << std::endl;

  switch (rqtype)
  {
  case RType::CONNECTION:
    return boost::shared_ptr<ConnectionServer> (new ConnectionServer (data));
  case RType::READY:
    return boost::shared_ptr<ReadyServer> (new ReadyServer (boost::shared_ptr<Client> (c_)));
  case RType::DISCONNECTION:
    disconnecting_ = true;
    return boost::shared_ptr<ClientIdDecorator> (new ClientIdDecorator (new Disconnection (), id_));
  case RType::GET_OBJECT:
    return boost::shared_ptr<ClientIdDecorator> (new ClientIdDecorator (new GetObjectServer (data), id_));
  case RType::GET_ALL_OBJECTS:
    return boost::shared_ptr<ClientIdDecorator> (new ClientIdDecorator (new GetAllObjects (), id_));
  case RType::TEXT:
    return boost::shared_ptr<Text> (new Text (data));
  case RType::UPDATE_OBJ:
    return boost::shared_ptr<ClientIdDecorator> (new ClientIdDecorator (new UpdateObj (data), id_));
  case RType::UPDATE_POS:
    return boost::shared_ptr<ClientIdDecorator> (new ClientIdDecorator (new UpdatePos (data), id_));
  case RType::GAME_MODE:
    return boost::shared_ptr<ClientIdDecorator> (new ClientIdDecorator (new GameMode (data), id_));
  case RType::MOVE:
    return boost::shared_ptr<ClientIdDecorator> (new ClientIdDecorator (new Move (data), id_));
  default:
    std::cout << "Error: unhandled request!" << rqtype << std::endl;
    return boost::shared_ptr<Request> ();
  }
}

boost::shared_ptr<Request>
RequestCreatorServer::on_error ()
{
  std::cout << "on error " << std::endl;
  return boost::shared_ptr<ClientIdDecorator> (new ClientIdDecorator (new Disconnection (), id_));
}
